<?php
//后台商品控制器
class GoodsController extends CommonController{
	
	//显示商品添加表单页面
	public function addAction(){
		$tip = I('tip','get','bool');
		$data = array();
		//查询分类数据
		$data['category'] = D('category')->getData('_tree');
		require ACTION_VIEW;
	}
	//商品修改
	public function editAction(){
		$id = I('id','get','id');
		$cid = I('cid','get','id');
		$page = I('page','get','id',1);
		$tip = I('tip','get','bool');
		$data = array();
		//查询分类数据
		$Category = D('category');
		$data['category'] = $Category->getData('_tree');
		//商品原数据
		$data['goods'] = D('goods')->getDataById($id);
		require ACTION_VIEW;
	}
	//添加处理
	public function addExecAction(){
		//接收表单数据
		$fields = array('category_id','name','sn','price','stock','on_sale','recommend','desc','album','thumb');
		$data = array();
		foreach($fields as $v){
			$data[$v] = $this->_input($v);
		}
		//为封面和相册生成缩略图
		$this->_createThumb($data['thumb']);
		//保存数据
		$Goods = D('goods');
		$id = $Goods->data($data)->add();
		//返回
		if(isset($_POST['return'])){
			$this->success('','/?p=admin&c=goods&a=index');
		}else{
			$this->success('','/?p=admin&c=goods&a=add&tip=1&id='.$id);
		}
	}
	//修改处理
	public function editExecAction(){
		$id = I('id','get','id');
		$cid = I('cid','get','id');
		$page = I('page','get','id',1);
		//接收表单数据
		$fields = array('category_id','name','sn','price','stock','on_sale','recommend','desc','album','thumb');
		$data = array('id'=>$id);
		foreach($fields as $v){
			$data[$v] = $this->_input($v);
		}
		//为封面和相册生成缩略图
		$this->_createThumb($data['thumb']);
		//保存数据
		$Goods = D('goods');
		$Goods->data($data)->save('id');
		//返回
		if(isset($_POST['return'])){
			$this->success('','/?p=admin&c=goods&a=index');
		}else{
			$this->success('',"/?p=admin&c=goods&a=edit&tip=1&id=$id&cid=$cid&page=$page");
		}
	}
	
	//商品列表
	public function indexAction(){
		$cid = I('cid','get','int',-1); //分类ID
		$page = I('page','get','id',1); //页码
		$data = array();
		//查询分类数据
		$Category = D('category');
		$data['category'] = $Category->getData('_tree');
		//如果分类ID大于0，则取出所有子分类ID
		$cids = ($cid>0) ? $Category->getSubIds($cid) : $cid;
		//获取商品列表
		$Goods = D('goods');
		$data['goods'] = $Goods->getData('goods',$cids,$page);
		//超出页码时自动返回第1页
		if(empty($data['goods']['data']) && $page>1){
			$this->redirect("/?p=admin&c=goods&a=index&cid=$cid");
		}
		require ACTION_VIEW;
	}
	
	//接收表单并进行验证
	private function _input($name){
		switch($name){
			case 'category_id'://分类ID
				$value = I('category_id','post','id');
			break;
			case 'name'://商品名称
				$value = I('name','post','text');
				($value=="" || mb_strlen($value)>40) && $this->error('商品名称不合法（1-40个字符）。');
			break;
			case 'sn'://商品编号
				$value = I('sn','post','text');
				preg_match('/^[0-9A-Za-z]{1,10}$/',$value) || $this->error('商品编号不合法（字母或数字，1-10个字符）。');
			break;
			case 'price'://商品价格
				$value = I('price','post','float');
				($value<0.01 || $value>100000) && $this->error('商品价格输入不合法（0.01~100000）');
			break;
			case 'stock'://商品库存
				$value = I('stock','post','int');
				($value<0 || $value>900000) && $this->error('商品库存输入不合法（0~900000）');
			break;
			case 'on_sale'://商品上架
				$value = I('on_sale','post','text');
				in_array($value,array('yes','no')) || $this->error('商品上架字段填写错误');
			break;
			case 'recommend'://商品推荐
				$value = I('recommend','post','text');
				in_array($value,array('yes','no')) || $this->error('商品推荐字段填写错误');
			break;
			case 'desc': //商品描述
				$value = I('desc','post','string');
				$value = HTMLPurifier($value); //富文本过滤
				isset($value[65535]) && $this->error('商品描述内容过多');
			break;	
			case 'album'://商品相册
				$value = I('album','post','');
				$value = htmlspecialchars(is_array($value) ? implode('|',$value) : '');
				isset($value[65535]) && $this->error('商品相册内容过多');
			break;
			case 'thumb'://商品封面图
				$value = I('thumb','post','text');
				isset($value[65535]) && $this->error('商品封面图内容过多');
			break;
		}
		return $value;
	}
	
	//修改单个字段状态值
	public function changeExecAction(){
		$id = I('id','post','id');
		$name = I('name','post','text');
		$value = I('value','post','text');
		$allow_name = array('recycle','on_sale','recommend');
		if(in_array($name,$allow_name)){
			D('goods')->change($id,$name,$value);
			$this->success();
		}else{
			$this->error('修改失败，指定字段不允许修改。');
		}
	}
	
	//为封面生成缩略图
	private function _createThumb($thumb){
		if($thumb==''){
			return false;
		}
		$filePath = "./public/upload/big/$thumb";   //原图的保存目录
		$savePath = "./public/upload/small/$thumb"; //封面图的保存目录
		//当图片不存在时创建
		if(!is_file($savePath)){
			try{
				$Image = new Image($filePath);
			}catch(Exception $e){
				$this->error($e->getMessage());
			}
			$Image->thumbFilled(220, 220)->save($savePath);
		}
	}
}